package apispace

import (
	"app/pkg/config"
	httpUtil "app/pkg/http"
	mailTools "app/pkg/mail"
	"encoding/json"
)

type TextFilterReply struct {
	Code    int     `json:"code"`
	Message string  `json:"message"`
	Data    string  `json:"data"`
	UseTime float64 `json:"use_time"`
}

type ApiSpaceClient struct {
}

func NewApiSpaceClient() *ApiSpaceClient {
	return &ApiSpaceClient{}
}

func (m *ApiSpaceClient) TextFilter(text, symbol string) (filter string, err error) {
	uri := "https://eolink.o.apispace.com/text-filters/api/v1/forward/text_filter/"
	bytes, err := httpUtil.Post(uri, map[string]string{
		"text":   text,
		"symbol": symbol,
	}, map[string]string{"X-APISpace-Token": "egcho4pdjtibkjwlmkge8msaf4jp2azv", "Authorization-Type": "apikey"}, 10)
	if err != nil {
		if err.Error() == "error http code 403" {
			mailTools.SendMail(config.Config.Mail.AdminNotify, "敏感词次数用尽")
		}
		return
	}
	if err == nil {
		tfr := TextFilterReply{}
		json.Unmarshal(bytes, &tfr)
		filter = tfr.Data
	} else {
		mailTools.SendMail(config.Config.Mail.AdminNotify, "敏感词接口错误")
	}
	return
}
